package cn.dreampie.security.encode;

import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by ice on 15-1-12.
 */
public class BASE64Encoder extends CharacterEncoder {
  private static final char[] pem_array = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};

  public BASE64Encoder() {
  }

  protected int bytesPerAtom() {
    return 3;
  }

  protected int bytesPerLine() {
    return 57;
  }

  protected void encodeAtom(OutputStream var1, byte[] var2, int var3, int var4) throws IOException {
    byte var5;
    if (var4 == 1) {
      var5 = var2[var3];
      byte var6 = 0;
      boolean var7 = false;
      var1.write(pem_array[var5 >>> 2 & 63]);
      var1.write(pem_array[(var5 << 4 & 48) + (var6 >>> 4 & 15)]);
      var1.write(61);
      var1.write(61);
    } else {
      byte var8;
      if (var4 == 2) {
        var5 = var2[var3];
        var8 = var2[var3 + 1];
        byte var9 = 0;
        var1.write(pem_array[var5 >>> 2 & 63]);
        var1.write(pem_array[(var5 << 4 & 48) + (var8 >>> 4 & 15)]);
        var1.write(pem_array[(var8 << 2 & 60) + (var9 >>> 6 & 3)]);
        var1.write(61);
      } else {
        var5 = var2[var3];
        var8 = var2[var3 + 1];
        byte var10 = var2[var3 + 2];
        var1.write(pem_array[var5 >>> 2 & 63]);
        var1.write(pem_array[(var5 << 4 & 48) + (var8 >>> 4 & 15)]);
        var1.write(pem_array[(var8 << 2 & 60) + (var10 >>> 6 & 3)]);
        var1.write(pem_array[var10 & 63]);
      }
    }

  }
}
